#!/usr/bin/env python
#-*-coding:utf-8-*-

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This project is sponsored by:
#   http://www.equiposcreativos.com/

################################################################################
# META
################################################################################

__version__ = "0.2"
__license__ = "GPL3"
__author__ = "JBC"
__since__ = "0.2"
__date__ = "2010/09/05"


################################################################################
# IMPORTS
################################################################################

import sys
import json
import unittest

from rssthief import rssthief


################################################################################
# TESTS
################################################################################

class NetworkTest(unittest.TestCase):
    
    def setUp(self):
        self.add_feed_json = json.dumps({"cmd": "add_feed",
                                         "feed":"http://fabio.com.ar/rss.php"})
        
        self.remove_feed_json = json.dumps({"cmd": "remove_feed",
                                            "feed":"http://fabio.com.ar/rss.php"})

        self.add_stopword_json = json.dumps({"cmd": "add_stopword",
                                             "word":"lala"})
        
        self.remove_stopword_json = json.dumps({"cmd": "remove_stopword",
                                                "word":"lala"})
        
        self.search_json = json.dumps({"cmd": "search",
                                       "query":"de",
                                       "toLang": "es",
                                       "agent": "pindonga",
                                       "replace": {"a":"b"}})
        
        self.status_json = json.dumps({"cmd": "status"})

    def _find_errors(self, response):
        data = json.loads(response)
        if data["status"] == "error":
            raise AssertionError(data["error"])
    
    def format_print(self, send, response):
        print "SEND:", send
        print "RESPONSE:", response
        print "*" * 40
    
    def test_add_feed(self):
        send = self.add_feed_json
        response = rssthief.main(send)
        self._find_errors(response)
        self.format_print(send, response)
    
    def test_remove_feed(self):
        send = self.remove_feed_json
        response = rssthief.main(send)
        self._find_errors(response)
        self.format_print(send, response)
        
    def test_add_stopword(self):
        send = self.add_stopword_json
        response = rssthief.main(send)
        self.format_print(send, response)
        
    def test_remove_stopword(self):
        send = self.remove_stopword_json
        response = rssthief.main(send)
        self._find_errors(response)
        self.format_print(send, response)
        
    def test_search(self):
        send = self.search_json
        response = rssthief.main(send)
        self._find_errors(response)
        self.format_print(send, response)
    
    def test_status(self):
        send = self.status_json
        response = rssthief.main(send)
        self._find_errors(response)
        self.format_print(send, response)


################################################################################
# MAIN
################################################################################

if __name__ == "__main__":
    unittest.main()

    
